package com.kerrykidz.system.util.createWord;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * 
  * @Description:(工具转换类)
  * @author 	ly
  * @datetime 	Dec 14, 2012 - 5:31:31 PM
  * @varsion 	BPO_V3.0
 */
public class ConvertUtil {
	/**
	 * 
	  * @Title: convertBean2Map
	  * @Description: TODO(将bean中数据转换)
	  * @param bean
	  * @return
	  * @throws IntrospectionException
	  * @throws IllegalAccessException
	  * @throws InvocationTargetException
	  * @return Map
	 */
	 @SuppressWarnings("unchecked")
	public static Map convertBean2Map(Object bean)  throws IntrospectionException, IllegalAccessException, InvocationTargetException { 
		  Class type = bean.getClass(); 
		  Map returnMap = new HashMap(); 
		  BeanInfo beanInfo = Introspector.getBeanInfo(type); 
		
		  PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors(); 
		  for (int i = 0; i< propertyDescriptors.length; i++) { 
		      PropertyDescriptor descriptor = propertyDescriptors[i]; 
		      String propertyName = descriptor.getName(); 
		      if (!propertyName.equals("class")) { 
		          Method readMethod = descriptor.getReadMethod(); 
		          Object result = readMethod.invoke(bean, new Object[0]); 
		          if (result != null) { 
		              returnMap.put(propertyName, result); 
		          } else { 
		              returnMap.put(propertyName, ""); 
		          } 
		      } 
		  } 
		  return returnMap; 
	} 
}
